package com.sense.kukoo.common.wiget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sense.kukoo.common.R;
import com.sense.kukoo.common.utils.ApplicationContext;
import com.sense.kukoo.lib_picture.loader.Monet;

public class HistoryItemView extends FrameLayout implements Checkable {

    private ImageView iv_music_icon;
    private TextView tv_music_des;
    private TextView tv_music_msg;
    private TextView tv_music_msg_2;
    private ImageView iv_is_selected;

    private boolean isEditMode = false;
    private boolean isSelected = false;
    private int color_select_on;
    private int color_select_off;
    private View root;

    public HistoryItemView(@NonNull Context context) {
        super(context);
        initView();
    }

    public HistoryItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public HistoryItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public HistoryItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.common_history_item, this, true);
        //
        root = findViewById(R.id.root);
        iv_music_icon = findViewById(R.id.iv_music_icon);
        tv_music_des = findViewById(R.id.tv_music_collection);
        tv_music_msg = findViewById(R.id.tv_music_msg);
        tv_music_msg_2 = findViewById(R.id.tv_music_msg_2);
        iv_is_selected = findViewById(R.id.iv_is_selected);
        color_select_on = ApplicationContext.getColor(R.color.common_bg_edit_select_on);
        color_select_off = ApplicationContext.getColor(R.color.common_bg_edit_select_off);
    }

    public void setEditMode(boolean editMode){
        isEditMode = editMode;
        if (isEditMode) {
            iv_is_selected.setVisibility(View.VISIBLE);
            setChecked(false);
        }else{
            iv_is_selected.setVisibility(View.GONE);
        }
    }

    @Override
    public void setChecked(boolean checked) {
        isSelected = checked;
        if (isSelected) {
            root.setBackgroundColor(color_select_on);
            iv_is_selected.setImageResource(R.drawable.common_icon_edit_select_on);
        }else{
            root.setBackgroundColor(color_select_off);
            iv_is_selected.setImageResource(R.drawable.common_icon_edit_select_off);
        }
    }

    @Override
    public boolean isChecked() {
        return isSelected;
    }

    @Override
    public void toggle() {
        setChecked(!isSelected);
        if (mOnCheckedChangedListener != null) {
            mOnCheckedChangedListener.onCheckedChanged();
        }
    }

    private OnCheckedChangedListener mOnCheckedChangedListener;

    public interface OnCheckedChangedListener{
        void onCheckedChanged();
    }

    public void setOnCheckedChangedListener(OnCheckedChangedListener l){
        mOnCheckedChangedListener = l;
    }

    public void setItemClickListener(OnClickListener l){
        root.setOnClickListener(l);
    }

    public void setItemLongClickListener(OnLongClickListener l){
        root.setOnLongClickListener(l);
    }

    public void setIconUrl(String url){
        Monet.get(ApplicationContext.instance())
                .load(url)
                .placeholder(R.mipmap.common_icon_default_editor_picks)
                .into(iv_music_icon);
    }

    public void setDes(String s){
        tv_music_des.setText(s);
    }

    public void setMsg(String s){
        tv_music_msg.setText(s);
    }

    public void setMsg2(String s){
        tv_music_msg_2.setText(s);
    }
}
